//
//  PlayAnimationView.m
//  EMMApp
//
//  Created by Mac on 16/11/2.
//  Copyright © 2016年 YiXun. All rights reserved.
//

#import "PlayAnimationView.h"

@interface PlayAnimationView ()

@property (nonatomic, strong) UIImageView *imageView;

@end

@implementation PlayAnimationView

- (id)initWithFrame:(CGRect)frame {
    if (self == [super initWithFrame:frame]) {
        [self showUI];
    }
    return  self;
}

- (void)showUI {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
    view.backgroundColor = [UIColor clearColor];
    view.userInteractionEnabled = YES;
    [self addSubview:view];
 
    UIView *subView = [[UIView alloc] initWithFrame:CGRectMake((self.bounds.size.width-150)/2, (self.bounds.size.height-150)/2, 150, 150)];
    subView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    subView.userInteractionEnabled = YES;
    subView.userInteractionEnabled = YES;
    subView.layer.masksToBounds = YES;
    subView.layer.cornerRadius = 5;
    [self addSubview:subView];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 120, subView.frame.size.width, 20)];
    label.font = [UIFont systemFontOfSize:15];
    label.text = @"正在播放";
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    [subView addSubview:label];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(subView.frame.size.width-25, 5, 20, 20);
    [btn setImage:[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnCLick:) forControlEvents:UIControlEventTouchUpInside];
    
    [subView addSubview:btn];
    
    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(25, 25, 100, 100)];
    [subView addSubview:_imageView];
    //创建一个数组，数组中按顺序添加要播放的图片（图片为静态的图片）
    NSMutableArray *imgArray = [NSMutableArray array];
    for (int i=1; i<4; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"playIm%d.png",i+1]];
        [imgArray addObject:image];
    }
    //把存有UIImage的数组赋给动画图片数组
    _imageView.animationImages = imgArray;
    //设置执行一次完整动画的时长
    _imageView.animationDuration = 4*0.3;
    //动画重复次数 （0为重复播放）
    _imageView.animationRepeatCount = 0;
    //开始播放动画
    [_imageView startAnimating];
    //停止播放动画  - (void)stopAnimating;
    //判断是否正在执行动画  - (BOOL)isAnimating;
}

- (void)btnCLick:(UIButton *)btn {
    [_imageView stopAnimating];
    if (_playAnimationBlock) {
        _playAnimationBlock();
    }
}
@end
